#!/usr/bin/env sh

Install_Docker() 
{
  Echo_Blue "安装 Docker..."

  # 检查安装工具
  if ! command -v curl >/dev/null 2>&1; then
    Install_Curl
  fi

  # 判断 docker 命令是否支持
  if command -v docker >/dev/null 2>&1; then
    read -p "更新 docker(y/N): " confirm
  else
    confirm="y"
  fi
  # 安装或更新 docker
  if [ "${confirm}" == "y" ]; then
    Update_Docker
  else
    Update_Docker_Version
  fi
}

Update_Docker()
{
  Echo_Green "安装 docker ..."

  if [ "${DISTRO}" = "Debian" ]; then
    Install_Docker_Debian
  fi
  if [ "${DISTRO}" = "Ubuntu" ]; then
    Install_Docker_Ubuntu
  fi

  # 配置镜像加速器地址及 DNS
  if [ -f /etc/default/docker ]; then # upstart 系统
    \cp /etc/default/docker ${DATA_PATH}/docker/docker_config_upstart_$(date +"%Y%m%d%H%M%S")
    Docker_Config_Upstart
  fi

  if [ -f /etc/docker/daemon.json ]; then # systemd 系统
    \cp /etc/docker/daemon.json /etc/docker/daemon_json_$(date +"%Y%m%d%H%M%S")
    Docker_Config_Systemd
  else
    Docker_Config_Systemd
  fi

  # vi /etc/resolv.conf
  # nameserver 223.5.5.5
  # nameserver 8.8.8.8
  # nameserver 8.8.4.4
  # /etc/init.d/networking restart

  Update_Docker_Version
}

Update_Docker_Version()
{
  # 更新版本号
  if [ -z ${DOCKER_VERSION} ]; then
    Get_Docker_Version
  fi
  sed -i -e "s|DOCKER_VERSION=.*|DOCKER_VERSION=${DOCKER_VERSION}|g;" ${INSTALL_CONF}
}

Get_Docker_Version()
{
  if command -v docker >/dev/null 2>&1; then
    DOCKER_VERSION=`docker --version`
    Echo_Green "当前版本: ${DOCKER_VERSION}"
    DOCKER_VERSION=${DOCKER_VERSION%,*}
    DOCKER_VERSION=${DOCKER_VERSION#Docker version }
  else
    Echo_Red "未安装 docker"
    exit 1
  fi
}

Install_Docker_Debian()
{
    curl -sSL https://get.daocloud.io/docker | sh
}

Install_Docker_Ubuntu()
{
    apt update
    apt install ca-certificates curl gnupg lsb-release # 依赖
    # 在apt上注册Docker的GPG密钥圈
    mkdir -p /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    chmod a+r /etc/apt/keyrings/docker.gpg
    # 把Docker包的源代码添加到系统中
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
    # 再次更新你的软件包列表
    apt update
    apt install docker-ce docker-ce-cli containerd.io
}

Set_Docker_Opts()
{
  Echo_Blue "镜像加速(Upstart)及 DNS 配置:"

  if [ -z "${DOCKER_REGISTRY_MIRROR}" ]; then
    CACHE="https://qdnaxo5d.mirror.aliyuncs.com"
    read -p "镜像加速地址(default=${CACHE}): " DOCKER_REGISTRY_MIRROR
  else
    echo "DOCKER_REGISTRY_MIRROR=${DOCKER_REGISTRY_MIRROR}"
  fi
  if [ -z "${DOCKER_REGISTRY_MIRROR}" ]; then
    DOCKER_REGISTRY_MIRROR=${CACHE}
  fi

  if [ -z "${DOCKER_CONTAINER_DNS}" ]; then
    CACHE="223.5.5.5"
    read -p "容器内置DNS(default=${CACHE}): " DOCKER_CONTAINER_DNS
  else
    echo "DOCKER_CONTAINER_DNS=${DOCKER_CONTAINER_DNS}"
  fi
  if [ -z "${DOCKER_CONTAINER_DNS}" ]; then
    DOCKER_CONTAINER_DNS=${CACHE}
  fi

  sed -i -e "s|DOCKER_REGISTRY_MIRROR=.*|DOCKER_REGISTRY_MIRROR=${DOCKER_REGISTRY_MIRROR}|g;" ${INSTALL_CONF}
  sed -i -e "s|DOCKER_CONTAINER_DNS=.*|DOCKER_CONTAINER_DNS=${DOCKER_CONTAINER_DNS}|g;" ${INSTALL_CONF}
}

Docker_Config_Upstart()
{
  Set_Docker_Opts

  cat > /etc/default/docker <<EOF
DOCKER_OPTS="--registry-mirror=${DOCKER_REGISTRY_MIRROR}"
DOCKER_OPTS="--dns ${DOCKER_CONTAINER_DNS} --dns 8.8.8.8"
EOF

  service docker restart
}

Docker_Config_Systemd()
{
  Set_Docker_Opts

  # 数据目录 data-root
  # 容器引擎 storage-driver
  # 镜像加速 registry-mirrors
  # 容器联网 dns
  cat > /etc/docker/daemon.json <<EOF
{
  "data-root": "${DATA_PATH}/docker/data/",
  "storage-driver": "devicemapper",
  "registry-mirrors": ["${DOCKER_REGISTRY_MIRROR}"],
  "dns": ["${DOCKER_CONTAINER_DNS}", "8.8.8.8"]
}
EOF

  # /etc/sysconfig/docker-storage
  # DOCKER_STORAGE_OPTIONS="--selinux-enabled --log-driver=journald --signature-verification=false"

  systemctl daemon-reload
  systemctl restart docker
}

Install_Docker_Compose()
{
  Echo_Blue "安装 Docker Compose ..."
  echo "查看最新 docker compose 版本: https://github.com/docker/compose/releases"

  # 判断 docker 命令是否支持 docker compose version
  if command -v docker compose >/dev/null 2>&1; then
    Get_Docker_Compose_Version
    read -p "已安装${DOCKER_COMPOSE_VERSION}, 是否更新？更新请输入版本号: " DOCKER_COMPOSE_VERSION
  else
    while [ -z "${DOCKER_COMPOSE_VERSION}" ]; do
      read -p "请输入版本号: " DOCKER_COMPOSE_VERSION
    done
  fi

  # 安装或更新 docker
  if [ ! -z "${DOCKER_COMPOSE_VERSION}" ]; then
    # 版本号前加 v
    DOCKER_COMPOSE_VERSION="v${DOCKER_COMPOSE_VERSION##*v}"
    # daocloud 国内源可能有问题
    # curl -L https://github.com/docker/compose/releases/download/v2.21.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
    curl -L https://get.daocloud.io/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
  fi

  Update_Docker_Compose_Version
}

Update_Docker_Compose_Version()
{
  # 更新版本号
  if [ -z ${DOCKER_COMPOSE_VERSION} ]; then
    Get_Docker_Compose_Version
  fi
  sed -i -e "s|DOCKER_COMPOSE_VERSION=.*|DOCKER_COMPOSE_VERSION=${DOCKER_COMPOSE_VERSION}|g;" ${INSTALL_CONF}
}

Get_Docker_Compose_Version()
{
  if command -v docker compose >/dev/null 2>&1; then
    DOCKER_COMPOSE_VERSION=`docker compose version`
    Echo_Green "当前版本: ${DOCKER_COMPOSE_VERSION}"
    DOCKER_COMPOSE_VERSION=${DOCKER_COMPOSE_VERSION#Docker Compose version }
  else
    Echo_Red "未安装 docker compose"
    exit 1
  fi
}

Check_Docker_Containers()
{
  Echo_Blue "检查运行中容器..."
  # echo `docker ps --format "{{.Names}}" | grep nginx`
  # 通过名称或端口查询容器，然后停止
  containers=`docker ps --format "{{.Names}}"`
  if [ ! -z "${containers}" ]; then
    Echo_Yellow "存在运行中的容器:"
    echo "${containers}"
    confirm="n"
    read -p "是否全部停止？(y/N)" confirm
    if [ "${confirm}" == "y" ]; then
      Echo_Yellow "正在停止..."
      for container in ${containers[*]};do
        docker stop ${container}
      done
    fi
  fi

  Echo_Blue "清理未使用数据..."
  docker system prune
}
